package top.lixunda.ecommerce.server.user.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.user.api.controller.IInitController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 初始化控制
 *
 * @author lixunda
 * @version time:2020/5/3 20:28
 */
@Slf4j
@RestController
public class InitController extends BaseController
        implements IInitController {

    private static final String KEY_INIT_SESSION_TIME = "init.sessionTime";

    /**
     * Session注册，防止session丢失导致的第一次无Cookie时的Session不一致问题
     *
     * @return 结果
     */
    @Override
    public MessageObject<String> sessionRegister() {
        this.getHttpSessionProxy().setAttribute(KEY_INIT_SESSION_TIME, LocalDateTime.now().format(
                DateTimeFormatter.BASIC_ISO_DATE));
        String time = (String) this.getHttpSessionProxy().getAttribute(KEY_INIT_SESSION_TIME);
        return getSuccessMessageObject("成功", time);
    }
}
